export function formatDate(fmt, date) {
  if (date === null || date === "") {
    return ''
  }
  date = new Date(date)

  let ret;
  const opt = {
    "Y+": date.getFullYear().toString(),        // 年
    "m+": (date.getMonth() + 1).toString(),     // 月
    "d+": date.getDate().toString(),            // 日
    "H+": date.getHours().toString(),           // 时
    "M+": date.getMinutes().toString(),         // 分
    "S+": date.getSeconds().toString()          // 秒
    // 有其他格式化字符需求可以继续添加，必须转化成字符串
  };
  for (let k in opt) {
    ret = new RegExp("(" + k + ")").exec(fmt);
    if (ret) {
      fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
    };
  };
  return fmt;
}
/**
 * desc   :  把时间戳转换成yyyy-mm-dd HH:mm:ss
 * use    :  dateFormat(时间戳)
 * return :  yyyy-mm-dd HH:mm:ss
 */
export function dateFormat(time) {
  if (!time) return
  let date = new Date(time);
  let year = date.getFullYear();
  let month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
  let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  let hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
  let minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
  let seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
  return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
}
export function dateFn(time) {
  if (!time) return
  let date = new Date(time),
    year = date.getFullYear(),
    month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1,
    day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  return year + "-" + month + "-" + day;
}
export function timestampToTime(timestamp) {
  var date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
  // var Y = date.getFullYear() + '-';
  // var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  // var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
  var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
  var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  // return Y + M + D + h + m + s;
  return h + m + s
}
/**
 * desc   :  获取两个时间的时间差 开始时间和结束时间需要是 yyyy-mm-dd HH:mm:ss
 * use    :  getTimeSalc(startTime, endTime)
 * return :  0 天 0 小时 0 分钟
 */
export function getTimeSalc(startTime, endTime) {
  if (!startTime || !endTime) {
    return
  }
  startTime = startTime.replace(new RegExp("-", "gm"), "/");
  let startTimeFormat = (new Date(startTime)).getTime(); //开始时间毫秒数

  endTime = endTime.replace(new RegExp("-", "gm"), "/");
  let endTimeFormat = (new Date(endTime)).getTime(); //结束时间毫秒数

  let millisecond1 = endTimeFormat - startTimeFormat; //获取相减的毫秒数；

  let days = Math.floor(millisecond1 / (24 * 3600 * 1000)); //获取天数；
  //如果天数小于0，则给其赋值00
  if (days < 0) days = "00"
  let millisecond2 = millisecond1 % (24 * 3600 * 1000);
  let hours = Math.floor(millisecond2 / (3600 * 1000)); //获取小时 //判断小时小于0  则赋值00 if(hours < 0){        hours="00" ; }
  let millisecond3 = millisecond2 % (3600 * 1000);
  let minutes = Math.floor(millisecond3 / (60 * 1000)); //获取分钟数
  if (minutes < 0) minutes = "00"
  return `${days}天 ${hours}小时 ${minutes}分钟`
}
/**
 *获取几个月前的输入日期
 *{param:DateTime} date 输入日期(YYYY-MM-DD)
 *{param:number } monthNum 月数
 * example: GetPreMonthDay('2015-02-22', 6)
 */
export function getPreMonthDay(date, monthNum) {
  let dateArr = date.split('-'),
    year = dateArr[0], //获取当前日期的年份
    month = dateArr[1], //获取当前日期的月份
    day = dateArr[2]; //获取当前日期的日
  let year2 = year,
    month2 = parseInt(month) - monthNum;
  if (month2 <= 0) {
    let absM = Math.abs(month2);
    year2 = parseInt(year2) - Math.ceil(absM / 12 == 0 ? 1 : parseInt(absM) / 12);
    month2 = 12 - (absM % 12);
  }

  let day2 = day,
    days2 = new Date(year2, month2, 0).getDate();
  if (day2 > days2) day2 = days2;
  if (month2 < 10) month2 = '0' + month2;
  return `${year2}-${month2}-${day2}`

}
